import { Header, Injectable, Req, Res } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
const svgCaptcha = require('svg-captcha');

@Injectable()
export class UsersService {
  create(createUserDto: CreateUserDto) {
    console.log(createUserDto, 'createUserDto');

    return 'This action adds a new user';
  }

  findAll() {
    return `user service 查询结果`;
  }

  findOne(id: number) {
    return `This action returns a #${id} user`;
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }

  createCode(req, res) {
    const captcha = svgCaptcha.create({
      size: 4, // 字符数
      ignoreChars: 'zxcvbnmasdfghjklqwertyuiop', // 过滤字符
      noise: 0, // 干扰线条数
      color: true,
      background: '#cc9966', // 背景颜色
    });
    req.session.code = captcha.text;
    res.type('image/svg+xml');
    res.send(captcha.data);
  }

  createUser(req, body, headers) {
    if (
      req.session.code.toLocaleLowerCase() === body.code.toLocaleLowerCase()
    ) {
      return {
        message: '验证码正确',
      };
    } else {
      return {
        message: '验证码错误',
      };
    }
  }
}
